var async = require('async');
var util = require('../utils/util.js');
var daoUtil = require('../utils/daoUtil');
var WorkDiaryClass = require('../class/workdiary.js');

module.exports = {

    create: function (params, cb) {
        var validate = daoUtil.validate(['title', 'startTime', 'finishTime','content','createUid'], params);
        if(!validate.valid){
            cb(validate.error);
            return;
        }
        daoUtil.proxy(function(models){
            var workdiary = new WorkDiaryClass();
            workdiary.init(params);
            models.workdiary.create(workdiary.getParams(), function (err, data) {
                if (err || !data) {
                    console.error(err);
                    cb(util.getSysError());
                }else{
                    cb(null, util.getSuccessRes(data));
                }
            });
        },cb);
    },
    list: function (params, cb) {
        if(params.userId){
            params.createUid = params.userId;
        }
        var validate = daoUtil.validate(['startTime', 'finishTime', 'createUid'], params);
        if (!validate.valid) {
            cb(validate.error);
            return;
        }
        daoUtil.proxy(function (models) {
            var startTime = new Date(params.startTime);
            var finishTime = new Date(params.finishTime);
            var query = {
                or: [{ startTime: {'>=': startTime, '<=': finishTime}}, {finishTime: {'>=': startTime, '<=': finishTime} },
                     { startTime: {'<=': startTime}, finishTime: {'>=': finishTime} }],
                createUid: params.createUid
            }
            models.workdiary.find(query).populate('createUid').exec(function (err, data) {
                if (err || !data) {
                    console.error(err);
                    cb(util.getSysError());
                } else {
                    cb(null, util.getSuccessRes(data));
                }
            });
        }, cb);
    },
    find: function (params, cb) {
        var validate = daoUtil.validate(['rowId','createUid'], params);
        if(!validate.valid){
            cb(validate.error);
            return;
        }
        daoUtil.proxy(function(models){
            var workdiary = new WorkDiaryClass();
            workdiary.init(params);
            models.workdiary.findOne(workdiary.getParams(), function (err, data) {
                if (err || !data) {
                    console.error(err);
                    cb(util.getSysError());
                }else{
                    cb(null, util.getSuccessRes(data));
                }
            });
        },cb);
    },
    update:function (params, cb) {
        var validate = daoUtil.validate(['title', 'startTime', 'finishTime','content','createUid','rowId'], params);
        if(!validate.valid){
            cb(validate.error);
            return;
        }
        daoUtil.proxy(function(models){
            var workdiary = new WorkDiaryClass();
            workdiary.init(params);
            models.workdiary.update({rowId:params.rowId,createUid:params.createUid},workdiary.getParams()).exec(function (err, data) {
                if (err || !data) {
                    console.error(err);
                    cb(util.getSysError());
                }else{
                    cb(null, util.getSuccessRes(data));
                }
            });
        },cb);
    },
    delete:function (params, cb) {
        var validate = daoUtil.validate(['createUid','rowId'], params);
        if(!validate.valid){
            cb(validate.error);
            return;
        }
        daoUtil.proxy(function(models){
            models.workdiary.destroy({rowId:params.rowId,createUid:params.createUid}).exec(function (err, data) {
                if (err || !data) {
                    console.error(err);
                    cb(util.getSysError());
                }else{
                    cb(null, util.getSuccessRes(data));
                }
            });
        },cb);
    }
};



